import { http } from 'utils'

export async function saveArticle(payload) {
  const path = payload.id ? 'imageText/update' : 'imageText/save'
  try {
    const res = await http.post(path, payload)
    if (res.code === 1) {
      return res
    }
    throw new Error(res.info)
  } catch (err) {
    throw new Error(err)
  }
}

export async function getArticleById(id) {
  try {
    const res = await http.get('imageText/info', { id })
    if (res.code === 1) {
      return res.data
    }
    throw new Error(res.info)
  } catch (err) {
    throw new Error(err)
  }
}

export async function getArticleList({ page = 1, pageSize = 10, gid = 0 }) {
  try {
    const res = await http.get('imageText/list', { page, pageSize, gid })
    if (res.code === 1) {
      return res.data
    }
    throw new Error(res.info)
  } catch (err) {
    throw new Error(err)
  }
}

export async function delArticle(id) {
  try {
    const res = await http.get('imageText/delete', { id })
    if (res.code === 1) {
      return res
    }
    throw new Error(res.info)
  } catch (err) {
    throw new Error(err)
  }
}

export async function getImgTextGroupList() {
  const res = await http.get('imageText/groupLists')
  return res.data
}


export async function deleteImgTextGroup(id) {
  try {
    const res = await http.get('imageText/deleteGroup', { id })
    if (res.code === 1) {
      return res
    }
    throw res.info
  } catch (err) {
    throw err
  }
}

export async function addImgTextGroup(name) {
  try {
    const res = await http.post('imageText/addGroup ', { name })
    if (res.code === 1) {
      return res
    }
    throw res.info
  } catch (err) {
    throw err
  }
}

export async function imgTextGrouping({ id, gid }) {
  try {
    const res = await http.post('imageText/grouping ', { id, gid })
    if (res.code === 1) {
      return res
    }
    throw res.info
  } catch (err) {
    throw err
  }
}
